/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	深圳市国省检日常化监管系统         </p>
 * <p>创建者   :	heyong 
 * 
 * <p>描   述  :   MaintainProjectServiceImpl.java for com.pengtu.gsj.service.impl.check.jsbz.scientificDecision    </p>
 * 
 * <p>最后修改 : $: 2017年8月7日-下午3:42:20 v 1.0.0	 win7   $ </p>
 * 
*/

package com.pengtu.gsj.service.impl.check.jsbz.scientificDecision;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.check.jsbz.scientificDecision.MaintainProjectDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.jsbz.MainTenanceProject;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.jsbz.scientificDecision.MaintainProjectService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;

/**
 * 
 * MaintainProjectServiceImpl
 * 
 * 2017年8月7日 下午3:42:20
 * 
 * @version 1.0.0
 * 
 */
@Service
@Transactional
public class MaintainProjectServiceImpl implements MaintainProjectService{
	
	@Autowired
	private SystemManageService systemManageService;
	
	@Autowired
	public MaintainProjectDao maintainProjectDao;
	
	@Autowired
	private CheckTableService checkTableService;
	
	public List<MainTenanceProject> getMaintainProjectList(QueryFilter queryFilter){
		return maintainProjectDao.find(queryFilter);
	}
	/**
	 * 查询，根据查询条件获取结果
	 */
	@Override
	public Map<String, Object> getMaintainProject(String startTime, String endTime, CheckTable checkTable, String tag)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		List<MainTenanceProject> maintainProjectList = new ArrayList<MainTenanceProject>();
		List<Attachment> list = new ArrayList<Attachment>();
		List<Object> values = new ArrayList<Object>();
		List<List<String>> resultLists = new ArrayList<List<String>>();
		List<String> checkResult = null;
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		double poitMark = Double.parseDouble((String)jsonObject.get("poitMark"));
		double reserveMark =  Double.parseDouble((String)jsonObject.get("reserveMark"));
		double firstStepScore =  Double.parseDouble((String)jsonObject.get("firstStepScore"));
		
		String projectNameId = (String)jsonObject.get("projectNameId");
		//取到所有文件
		list = systemManageService.getCheckAttachmentsByPartId(startTime, endTime, tag, projectNameId);
		//查询数据
		QueryFilter queryFilter = new QueryFilter();
		queryFilter.addFilter("year", startTime, MatchType.GE);
		queryFilter.addFilter("year", endTime, MatchType.LE);
		//检查记录
		double total = 0.0;				//得分
		double percentage = 0.0;		//完善度
		double tableFull = MathsUtils.sumDouble(yearFull, poitMark);
		tableFull = MathsUtils.sumDouble(tableFull, firstStepScore);
		tableFull = MathsUtils.mulDouble(tableFull, 8);         //满分
		
		if (StringUtils.isEmpty(projectNameId)){
			maintainProjectList = this.getMaintainProjectList(queryFilter);
		} else {
			values.add(projectNameId);
			String[] idArrs = projectNameId.split(",");
			for (int i = 0; i < idArrs.length; i++) {
				checkResult = Arrays.asList("0");
				double temp = 0;
				MainTenanceProject mainTenanceProject = maintainProjectDao.get(idArrs[i]);
				
				if (mainTenanceProject.getDiagnoses().equals("1")) {
					total += yearFull;
					temp += yearFull;
				} else if (mainTenanceProject.getDiagnoses().equals("2")) {
					total += yearPart;
					temp += yearPart;
				}
				
				if (mainTenanceProject.getMaintenancePlan().equals("1")) {
					total += poitMark;
					temp += poitMark;
				} else if (mainTenanceProject.getMaintenancePlan().equals("2")) {
					total += reserveMark;
					temp += reserveMark;
				}
				
				if (mainTenanceProject.getMaintenanceEffect().equals("1")) {
					total += firstStepScore;
					temp += firstStepScore;
				}
				checkResult.set(0, String.valueOf(MathsUtils.formatDouble(temp, 1)));
				resultLists.add(checkResult);
				
				maintainProjectList.add(mainTenanceProject);
				
			}
			map.put("sum", MathsUtils.formatDouble(total, 1));
			//判断完善度
			percentage = MathsUtils.divDouble(total, tableFull, 2);
			percentage = MathsUtils.mulDouble(percentage, 100);
			map.put("percent", percentage);
			
			//返回时间
			int years[] = new int[Integer.parseInt(endTime) - Integer.parseInt(startTime)+1];
			for (int i = 0; i < years.length; i++) {
				years[i] = Integer.parseInt(startTime)+i;
			}
			map.put("years", years);
		}
		
		//得到所有文件
		list = systemManageService.getAllCheckAttachments(startTime, endTime, tag);
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("att", list);
		map.put("resultLists", resultLists);
		map.put("checkTable", checkTable);
		map.put("changeInves", maintainProjectList);
		map.put("score", checkTable.getTestFull());
		return map;
	}
}
